﻿namespace LiteBlog.Repositories
{
    using System;
    using System.Xml.Linq;

    using LiteBlog.Common;
    using LiteBlog.Common.Components;
    using LiteBlog.Common.Entities;
    using LiteBlog.Repositories.Contracts;
    using LiteBlog.Repositories.Helpers;
    using LiteBlog.Common.Enums;
    using LiteBlog.Repositories.Framework;
    using LiteBlog.Repositories.Common;

    public class SettingsRepository : DataAccess, ISettingsRepository
    {
        private static TimeZoneInfo _tzi = null;

        public SettingsRepository(IDataContext context, IXmlHelper xmlHelper) : base(context, xmlHelper)
        {
            _path = context.GetPath(DataStoreType.Settings, null);
        }

        public Settings Load()
        {
            var settings = new Settings();
            var root = _xmlHelper.Load(_path);

            settings.Name = (string)root.Element(Elem.Name);
            settings.Tagline = (string)root.Element(Elem.Tagline) ?? string.Empty;
            settings.UseImage = (bool)root.Element(Elem.UseImage);
            settings.ImageUrl = (string)root.Element(Elem.ImageUrl);
            settings.Url = root.Element(Elem.Url).Value;
            settings.PostCount = (int)root.Element(Elem.PostCount);
            settings.GATrackingCode = (string)root.Element(Elem.GATrackingCode) ?? string.Empty;
            settings.CommentModeration = (bool)root.Element(Elem.CommentModeration);
            settings.Timezone = root.Element(Elem.Timezone).Value;
            settings.Skin = root.Element(Elem.Skin).Value;
            settings.LogFile = root.Element(Elem.LogFile).Value;
            settings.LogLevel = root.Element(Elem.LogLevel).Value;
            settings.AppKey = root.Element(Elem.AppKey).Value;
            settings.AppSecret = root.Element(Elem.AppSecret).Value;
            settings.AccessToken = root.Element(Elem.AccessToken).Value;
            settings.ShouldBackup = (bool)root.Element(Elem.ShouldBackup);
            return settings;
        }

        public void Save(Settings settings)
        {
            var root = _xmlHelper.Load(_path);

            var settingsElem = new XElement(
                    Elem.Settings,
                    new XElement(Elem.Name, settings.Name),
                    new XElement(Elem.Tagline, settings.Tagline),
                    new XElement(Elem.UseImage, settings.UseImage),
                    new XElement(Elem.ImageUrl, settings.ImageUrl),
                    new XElement(Elem.Url, settings.Url),
                    new XElement(Elem.PostCount, settings.PostCount),
                    new XElement(Elem.Timezone, settings.Timezone),
                    new XElement(Elem.GATrackingCode, settings.GATrackingCode),
                    new XElement(Elem.CommentModeration, settings.CommentModeration),
                    new XElement(Elem.Skin, settings.Skin),
                    new XElement(Elem.LogFile, settings.LogFile),
                    new XElement(Elem.LogLevel, settings.LogLevel),
                    new XElement(Elem.AppKey, settings.AppKey),
                    new XElement(Elem.AppSecret, settings.AppSecret),
                    new XElement(Elem.AccessToken, settings.AccessToken),
                    new XElement(Elem.ShouldBackup, settings.ShouldBackup)
                    );

            root.ReplaceNodes(settingsElem.Elements());
            _xmlHelper.Save(root, _path);
        }

        internal TimeZoneInfo TimeZoneInfo
        {
            get
            {
                if (_tzi == null)
                {
                    _tzi = TimeZoneInfo.FindSystemTimeZoneById(GetTimeZoneInfo());
                }

                return _tzi;
            }
        }

        private string GetTimeZoneInfo()
        {
            var settings = Load();
            return settings.Timezone;
        }
    }
}